/// 运动记录数据模型
class SportRecord {
  /// 记录ID
  final int id;
  
  /// 戒指设备ID（设备序列号）
  final String deviceSn;
  
  /// 步数
  final int steps;
  
  /// 距离（米）
  final int distance;
  
  /// 卡路里
  final int calories;
  
  /// 运动时间（分钟）
  final int sportTime;
  
  /// 创建时间
  final String createTime;
  
  /// 参数
  final Map<String, dynamic> params;

  const SportRecord({
    required this.id,
    required this.deviceSn,
    required this.steps,
    required this.distance,
    required this.calories,
    required this.sportTime,
    required this.createTime,
    required this.params,
  });

  /// 从JSON创建SportRecord实例
  factory SportRecord.fromJson(Map<String, dynamic> json) {
    return SportRecord(
      id: _parseInt(json['id']) ?? 0,
      deviceSn: json['deviceSn']?.toString() ?? '',
      steps: _parseInt(json['steps']) ?? 0,
      distance: _parseInt(json['distance']) ?? 0,
      calories: _parseInt(json['calories']) ?? 0,
      sportTime: _parseInt(json['sportTime']) ?? 0,
      createTime: json['createTime']?.toString() ?? '',
      params: json['params'] as Map<String, dynamic>? ?? {},
    );
  }

  /// 安全解析整数（支持String和int类型）
  static int? _parseInt(dynamic value) {
    if (value == null) return null;
    if (value is int) return value;
    if (value is String) return int.tryParse(value);
    return null;
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'deviceSn': deviceSn,
      'steps': steps,
      'distance': distance,
      'calories': calories,
      'sportTime': sportTime,
      'createTime': createTime,
      'params': params,
    };
  }

  @override
  String toString() {
    return 'SportRecord{id: $id, deviceSn: $deviceSn, steps: $steps, distance: ${distance}m, calories: $calories, sportTime: $sportTime分钟, createTime: $createTime}';
  }
}
